<template>
    <div class="mod-category">
        <el-form :inline="true"
                 :model="dataForm">
            <el-form-item>
                <el-button @click="addOrUpdateHandle()"
                           icon="el-icon-plus"
                           size="small"
                           type="primary"
                           v-if="isAuth('prod:category:save')">新增
                </el-button>
            </el-form-item>
        </el-form>
        <el-table :data="dataList"
                  border
                  row-key="categoryId"
                  style="width: 100%;">

            <el-table-column header-align="center"
                             label="分类名称"
                             prop="categoryName"
                             treeKey="categoryId"
                             width="150">
            </el-table-column>
            <el-table-column align="center"
                             header-align="center"
                             label="图片"
                             prop="pic">
                <template slot-scope="scope">
                    <img :src="resourcesUrl + scope.row.pic "/>
                </template>
            </el-table-column>
            <el-table-column align="center"
                             header-align="center"
                             label="状态"
                             prop="status">
                <template slot-scope="scope">
                    <el-tag size="small"
                            type="danger"
                            v-if="scope.row.status === 0">下线
                    </el-tag>
                    <el-tag size="small"
                            v-else>正常
                    </el-tag>
                </template>
            </el-table-column>
            <el-table-column align="center"
                             header-align="center"
                             label="排序号"
                             prop="seq">
            </el-table-column>
            <el-table-column
                    align="center"
                    header-align="center"
                    label="操作">
                <template slot-scope="scope">
                    <el-button @click="addOrUpdateHandle(scope.row.categoryId)"
                               size="small"
                               type="primary"
                               v-if="isAuth('prod:category:update')">修改
                    </el-button>
                    <el-button @click="deleteHandle(scope.row.categoryId)"
                               size="small"
                               type="danger"
                               v-if="isAuth('prod:category:delete')">删除
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- 弹窗, 新增 / 修改 -->
        <add-or-update @refreshDataList="getDataList"
                       ref="addOrUpdate"
                       v-if="addOrUpdateVisible"></add-or-update>
    </div>
</template>

<script>
    import AddOrUpdate from './category-add-or-update'
    import {treeDataTranslate} from '@/utils'

    export default {
        data() {
            return {
                dataForm: {},
                dataList: [],
                dataListLoading: false,
                addOrUpdateVisible: false,
                resourcesUrl: process.env.VUE_APP_RESOURCES_URL
            }
        },
        components: {
            AddOrUpdate
        },
        activated() {
            this.getDataList()
        },
        methods: {
            // 获取数据列表
            getDataList() {
                this.dataListLoading = true
                this.$http({
                    url: this.$http.adornUrl('/prod/category/table'),
                    method: 'get',
                    params: this.$http.adornParams()
                }).then(({data}) => {
                    this.dataList = treeDataTranslate(data, 'categoryId', 'parentId')
                    this.dataListLoading = false
                })
            },
            // 新增 / 修改
            addOrUpdateHandle(id) {
                this.addOrUpdateVisible = true
                this.$nextTick(() => {
                    this.$refs.addOrUpdate.init(id)
                })
            },
            // 删除
            deleteHandle(id) {
                this.$confirm(`确定进行删除操作?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$http({
                        url: this.$http.adornUrl(`/prod/category/${id}`),
                        method: 'delete',
                        data: this.$http.adornData()
                    }).then(({data}) => {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            duration: 1500,
                            onClose: () => {
                                this.getDataList()
                            }
                        })
                    })
                })
            }
        }
    }
</script>
<style lang="scss">
    .mod-category {
        img {
            height: 80px;
            width: 200px;
        }
    }
</style>
